package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dao.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;



    @Override
    public R<String> removeSelf(Long id) {

        //判断是否关联dish,根据id查询是否有数据,有就返回未知错误信息
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(Dish::getCategoryId,id);
        long count1 = dishService.count(dqw);
        if (count1>0){
            //有关联
            log.info("与菜品有关联");
            return R.fail("当前分类下关联了菜品,不能删除");
        }

        //判断是否关联setmeal
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.eq(Setmeal::getCategoryId,id);
        long count2 = setmealService.count(sqw);
        if (count2>0){
            //有关联
            log.info("与套餐有关联");
            return R.fail("当前分类下关联了套餐,不能删除");
        }
        //都无关联,可删除
        super.removeById(id);
        log.info("都无关联,删除成功");
        return R.success("删除成功");
    }
}
